
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>字符串 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="函数.html" />
    
    
    <link rel="prev" href="解构.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="变量.html">
            
                <a href="变量.html">
            
                    
                    变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="解构.html">
            
                <a href="解构.html">
            
                    
                    解构
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.4" data-path="字符串.html">
            
                <a href="字符串.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="函数.html">
            
                <a href="函数.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="数组.html">
            
                <a href="数组.html">
            
                    
                    数组
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="对象.html">
            
                <a href="对象.html">
            
                    
                    对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="prototype.html">
            
                <a href="prototype.html">
            
                    
                    prototype
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="Promise.html">
            
                <a href="Promise.html">
            
                    
                    Promise
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="模块化.html">
            
                <a href="模块化.html">
            
                    
                    模块化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="class和继承.html">
            
                <a href="class和继承.html">
            
                    
                    class和继承
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="this.html">
            
                <a href="this.html">
            
                    
                    this
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="Symbol和generator.html">
            
                <a href="Symbol和generator.html">
            
                    
                    Symbol和generator
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="async_await.html">
            
                <a href="async_await.html">
            
                    
                    async_await
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="Set和WeakSet.html">
            
                <a href="Set和WeakSet.html">
            
                    
                    Set和WeakSet
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="Map和WeakMap.html">
            
                <a href="Map和WeakMap.html">
            
                    
                    Map和WeakMap
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="数字和Math.html">
            
                <a href="数字和Math.html">
            
                    
                    数字和Math
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="正则.html">
            
                <a href="正则.html">
            
                    
                    正则
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19" data-path="Proxy.html">
            
                <a href="Proxy.html">
            
                    
                    Proxy
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >字符串</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x5B57;&#x7B26;&#x4E32;">&#x5B57;&#x7B26;&#x4E32;</h1>
<h3 id="&#x5B57;&#x7B26;&#x4E32;&#x8FDE;&#x63A5;">&#x5B57;&#x7B26;&#x4E32;&#x8FDE;&#x63A5;</h3>
<pre><code class="lang-js">&lt;script type=<span class="hljs-string">&quot;text/javascript&quot;</span>&gt;
    <span class="hljs-keyword">let</span> name = <span class="hljs-string">&apos;Strive&apos;</span>;
    <span class="hljs-keyword">let</span> age = <span class="hljs-number">58</span>;
    <span class="hljs-comment">// let str = &apos;&#x8FD9;&#x4E2A;&#x4EBA;&#x662F;&apos;+name+&apos;,&#x5E74;&#x9F84;&#x662F; &apos; + age + &apos;&#x5C81;&apos;;</span>
    <span class="hljs-keyword">let</span> str = <span class="hljs-string">`&#x8FD9;&#x4E2A;&#x4EBA;&#x662F;<span class="hljs-subst">${name}</span>, &#x5E74;&#x9F84;&#x662F;<span class="hljs-subst">${age}</span>&#x5C81;`</span>;
    <span class="hljs-built_in">console</span>.log(str);
<span class="xml"><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span></span>
</code></pre>
<h3 id="&#x5B57;&#x7B26;&#x4E32;&#x8FDE;&#x63A5;&#x5B9E;&#x4F8B;">&#x5B57;&#x7B26;&#x4E32;&#x8FDE;&#x63A5;&#x5B9E;&#x4F8B;</h3>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE html&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>var<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">mete</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;viewport&quot;</span> <span class="hljs-attr">content</span>=<span class="hljs-string">&quot;width=device-width, initial-scale=1&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">meter</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text/javascript&quot;</span>&gt;</span><span class="javascript">
        <span class="hljs-keyword">let</span> data = [
            {title:<span class="hljs-string">&apos;&#x4E2D;&#x56FD;&apos;</span>, read:<span class="hljs-number">900</span>},
            {title:<span class="hljs-string">&apos;&#x7F8E;&#x56FD;&apos;</span>, read:<span class="hljs-number">800</span>},
            {title:<span class="hljs-string">&apos;&#x6CD5;&#x56FD;&apos;</span>, read:<span class="hljs-number">200</span>},
            {title:<span class="hljs-string">&apos;&#x6FB3;&#x5927;&#x5229;&#x4E9A;&apos;</span>, read:<span class="hljs-number">300</span>},
        ];
        <span class="hljs-built_in">window</span>.onload = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
            <span class="hljs-keyword">let</span> ul1 = <span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">&quot;#ul1&quot;</span>);
            <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; data.length; i++) {
                <span class="hljs-keyword">let</span> li = <span class="hljs-built_in">document</span>.createElement(<span class="hljs-string">&apos;li&apos;</span>);
                li.innerHTML = 
                <span class="hljs-string">`&lt;span&gt;<span class="hljs-subst">${data[i].title}</span>&lt;/span&gt;
                &lt;span&gt;&#x9605;&#x8BFB;&#x4EBA;&#x6570;:<span class="hljs-subst">${data[i].read}</span>&lt;/span&gt;
                &lt;a href=&quot;javascript:;&quot;&gt;&#x8BE6;&#x60C5;&lt;/a&gt;`</span>;
                ul1.appendChild(li);
            }
        }
    </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;ul1&quot;</span>&gt;</span>
        <span class="hljs-comment">&lt;!-- &lt;li&gt;
            &lt;span&gt;&#x8FD9;&#x662F;&#x65B0;&#x95FB;&#x7684;&#x6807;&#x9898;&lt;/span&gt;
            &lt;span&gt;&#x9605;&#x8BFB;&#x4EBA;&#x6570;:10&lt;/span&gt;
            &lt;a href=&quot;javascript:;&quot;&gt;&#x8BE6;&#x60C5;&lt;/a&gt;
        &lt;/li&gt; --&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<h3 id="&#x5B57;&#x7B26;&#x4E32;&#x67E5;&#x627E;&#x5224;&#x65AD;">&#x5B57;&#x7B26;&#x4E32;&#x67E5;&#x627E;&#x5224;&#x65AD;</h3>
<pre><code class="lang-js">&lt;script type=<span class="hljs-string">&quot;text/javascript&quot;</span>&gt;
    <span class="hljs-keyword">let</span> str = <span class="hljs-string">&apos;apple banana pear&apos;</span>;
    <span class="hljs-comment">// if (str.indexOf(&apos;banana&apos;) != -1) {</span>
    <span class="hljs-comment">//     alert(true);</span>
    <span class="hljs-comment">// } else {</span>
    <span class="hljs-comment">//     alert(false);</span>
    <span class="hljs-comment">// }</span>

    alert(str.includes(<span class="hljs-string">&apos;banana&apos;</span>));
<span class="xml"><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span></span>
</code></pre>
<h3 id="&#x67E5;&#x627E;&#x6D4F;&#x89C8;&#x5668;">&#x67E5;&#x627E;&#x6D4F;&#x89C8;&#x5668;</h3>
<pre><code class="lang-js">&lt;script type=&quot;text/javascript&quot;&gt;
    if (navigator.userAgent.includes(&apos;Chrome&apos;)) {
        alert(&apos;Chrome&apos;);
    } else {
        alert(&apos;Not Chrome&apos;);
    }
&lt;/script&gt;
</code></pre>
<h3 id="&#x5B57;&#x7B26;&#x4E32;&#x4EE5;&#x5F00;&#x5934;&#x548C;&#x7ED3;&#x5C3E;">&#x5B57;&#x7B26;&#x4E32;&#x4EE5;...&#x5F00;&#x5934;&#x548C;&#x7ED3;&#x5C3E;</h3>
<pre><code class="lang-js">&lt;script type=&quot;text/javascript&quot;&gt;
    let str = &apos;file:///Users/daliu/Documents/Processing/ES6/var.html&apos;;
    let str2 = &apos;https://www.baidu.com/&apos;;
    console.log(str.startsWith(&apos;http&apos;)); // false
    console.log(str2.startsWith(&apos;http&apos;)); // true

    let str3 = &quot;aaa.png&quot;;
    console.log(str3.endsWith(&apos;.png&apos;)); // true
&lt;/script&gt;
</code></pre>
<h3 id="&#x5B57;&#x7B26;&#x4E32;&#x586B;&#x5145;">&#x5B57;&#x7B26;&#x4E32;&#x586B;&#x5145;</h3>
<pre><code class="lang-js">&lt;script type=&quot;text/javascript&quot;&gt;
    let hello = &apos;hello &apos;;
    console.log(hello.repeat(30)); // &#x91CD;&#x590D;&#x5B57;&#x7B26;&#x4E32;

    //str.padStart(&#x6574;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#x957F;&#x5EA6;, &#x8981;&#x586B;&#x5145;&#x7684;&#x5B57;&#x7B26;&#x4E32;);  &#x8868;&#x793A;&#x5F80;&#x524D;&#x586B;&#x5145;
    //&#x540C;&#x7406; str.padEnd(&#x6574;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#x957F;&#x5EA6;, &#x8981;&#x586B;&#x5145;&#x7684;&#x5B57;&#x7B26;&#x4E32;); &#x8868;&#x793A;&#x5F80;&#x540E;&#x586B;&#x5145;
    //a -&gt; xxxa
    console.log(&apos;a&apos;.padStart(4, &apos;xxx&apos;)); // xxxa

    let str = &apos;a&apos;;
    let padStr = &apos;bbb&apos;;
    let length = str.length + padStr.length;
    console.log(str.padStart(length, &apos;bbb&apos;)); // bbba
&lt;/script&gt;
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="解构.html" class="navigation navigation-prev " aria-label="Previous page: 解构">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="函数.html" class="navigation navigation-next " aria-label="Next page: 函数">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"字符串","level":"1.4","depth":1,"next":{"title":"函数","level":"1.5","depth":1,"path":"函数.md","ref":"函数.md","articles":[]},"previous":{"title":"解构","level":"1.3","depth":1,"path":"解构.md","ref":"解构.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"字符串.md","mtime":"2021-11-15T14:00:54.483Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2022-02-17T15:54:39.156Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

